package org.example.mybatis.plus.boot3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mybatis.plus.boot3.entity.User;
import org.example.mybatis.plus.boot3.mapper.UserMapper;
import org.example.mybatis.plus.boot3.service.UserService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public List<User> getByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        return this.list(wrapper);
    }

    @Override
    public List<User> getByNameWithLambda(String name) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getName, name);
        return this.list(wrapper);
    }

    @Override
    public boolean updateByName(String name, String newName) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name", name)
                .set("name", newName)
                .set("update_time", new Date());
        return this.update(updateWrapper);
    }

    @Override
    public boolean updateByNameWithLambda(String name, String newName) {
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getName, name)
                .set(User::getName, newName)
                .set(User::getUpdateTime, new Date());
        return this.update(wrapper);
    }

    @Override
    public void wrapper() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", 1);
        map.put("name", "老王");
        map.put("update_time", null);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //wrapper.allEq(true,(field, value) -> field.contains("a"),map, true);
        wrapper.select("name")
                .groupBy("name")
                .ge("id",1)
                .having("count(1)>1")
                .orderByAsc("name");
        this.list(wrapper);

    }


}
